<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

<!-- head 头部 -->
<head th:replace="admin/layout/include::head('订单管理','')"></head>

<body>
<!-- loading 动画-->
<div th:replace="admin/layout/include::loading"></div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="pull-left layui-form-pane search-box">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input id="edtSearch" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">商品名称：</label>
                        <div class="layui-input-inline mr0" style="width: 140px;">
                            <select id="goodsId">
                                <option value=""></option>
                                <option th:each="goods:${goodsList}" th:value="${goods.goodsId}" th:text="${goods.goodsName}">元宝100</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">状态：</label>
                        <div class="layui-input-inline mr0" style="width: 140px;">
                            <select id="state">
                                <option value=""></option>
                                <option value="0">支付成功</option>
                                <option value="1">正在支付</option>
                                <option value="2">订单取消</option>
                                <option value="3">支付超时</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
                      <!--  <button id="btnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>-->
                    </div>
                </div>
            </div>

            <!-- 数据表格 -->
            <table class="layui-table" id="data-table" lay-filter="data-table"></table>
        </div>
    </div>
</div>
</body>

<!-- 表格操作列 -->
<script type="text/html" id="data-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" ajax-event="del" ajax-url="/admin/order/" value="{{d.orderId}}">删除</a>
    <a class="layui-btn layui-btn-xs" data-dropdown="#dropOperate{{d.LAY_INDEX}}" no-shade="true">
        状态<i class="layui-icon layui-icon-drop" style="font-size: 12px;margin-right: 0;"></i></a>
    <!-- 下拉菜单 -->
    <ul class="dropdown-menu-nav dropdown-bottom-right layui-hide" id="dropOperate{{d.LAY_INDEX}}">
        <div class="dropdown-anchor"></div>
        <li><a lay-event="payCancel"><i class="layui-icon layui-icon-close"></i>取消订单</a></li>
        <li><a lay-event="paySuccess"><i class="layui-icon layui-icon-ok"></i>支付成功</a></li>
        <li><a lay-event="paying"><i class="layui-icon layui-icon-loading"></i>正在支付</a></li>
        <li><a lay-event="payTimeout"><i class="layui-icon layui-icon-log"></i>支付超时</a></li>
    </ul>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="data-table-state">
    <!--类型-->
    {{#  if(d.state == 0){ }}
    <span style="color: #009688;">支付成功</span>
    {{#  } else if(d.state == 1){ }}
    <span style="color: #5FB878;">正在支付</span>
    {{#  } else if(d.state == 2){ }}
    <span style="color: #1E9FFF;">订单取消</span>
    {{#  } else if(d.state == 3){ }}
    <span style="color: #ff7714;">支付超时</span>
    {{#  } else{ }}
    unkown
    {{#  }}}
</script>
<!--类型-->
<script type="text/html" id="toolbarOepration">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add"><i
                class="layui-icon">&#xe654;</i>添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" ajax-event="batchDel" ajax-url="/admin/order/" ajax-id="orderId">
            <i class="layui-icon layui-icon-delete"></i>批量删除</button>
    </div>
</script>
<!-- js部分 -->
<div th:replace="admin/layout/include::js"></div>
<script src="https://cdn.bootcss.com/clipboard.js/2.0.4/clipboard.min.js"></script>
<script>
    layui.use(['form', 'table', 'util', 'admin','dropdown'], function () {
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var layer = layui.layer;
        var dropdown = layui.dropdown;
        var formUrl;  // 表单提交的action

        //渲染表格
        var tableIns = table.render({
            elem: '#data-table',
            url: '/admin/order/',
            page : true,
            toolbar: '#toolbarOepration',
			loading : true,
			height : 'full-115',
			limit : 10,
			limits : [ 10, 20, 50, 100 ],
            response:{
	        	countName: 'total'
	        },
            cols: [
                [
                {type:'checkbox'},
                {field: 'orderId', title: '#',width:180},
                {field: 'payNumber', title: '支付编码',width:130},
                {field: 'userName', title: '用户名',width:80},
                {field: 'goodsName',title:'商品名称',width:120},
                {field: 'payType', title: '支付类型',width:100,templet: function (d) {
                        return d.payType=='alipay'?'支付宝':d.payType=='weixin'?'微信':'其他';
                }},
                {field: 'money',title:'金额',width:60},
                {field: 'realMoney',title:'实际金额',width:60},
                {field: 'state', title: '状态',width:100,templet: '#data-table-state'},
                {
                    field: 'createTime',width:160, sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {align: 'center',width:180, toolbar: '#data-table-bar', title: '操作'}
            ]
            ],
            text: {
                none: '暂无相关数据'
            }
        });

        // 添加按钮点击事件
        $('#btnAdd').click(function () {
            showEditModel();
        });


        // 工具条点击事件
        table.on('tool(data-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { //删除
                doDelete(obj);
            }else if(obj.event ==='payCancel'){
                updateState(obj,2);
            }else if(obj.event ==='paySuccess'){
                updateState(obj,0);
            }else if(obj.event ==='paying'){
                updateState(obj,1);
            }else if(obj.event ==='payTimeout'){
                updateState(obj,3);
            }
        });

        // 搜索按钮点击事件
        $('#btnSearch').click(function () {
            var keyword = $('#edOrtSearch').val();
            var param = {
                keyword:keyword,
                goodsId:$('#goodsId').val(),
                state:$('#state').val()
            }
            tableIns.reload( {where: param},'data');
        });
        function doDelete(obj){
            top.layer.confirm('确定要删除吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            },function (i) {
                top.layer.close(i);
                B.del({
                    url:'/admin/order/'+obj.data.orderId,
                    loading:true,
                    success:function(data){
                        if (data.success) {
                            layer.msg(data.msg, {icon: 1});
                            obj.del();
                        } else {
                            layer.msg(data.msg, {icon: 2});
                        }
                    }
                });
            });
        }

        function updateState(obj,state) {
            B.post({
                url: '/admin/order/updateState',
                loading: true,
                data: {orderId: obj.data.orderId,state:state},
                success: function (data) {
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                        tableIns.reload({}, 'data');
                    } else {
                        layer.msg(data.msg, {icon: 2});
                    }
                }

            });
        }
        // 显示编辑弹窗
        var showEditModel = function (data) {
            admin.putTempData("t_page",data);
            admin.putTempData("formOk",false);
            var index = layer.open({
                type: 2,
                title: data ? '修改页面' : '添加页面',
                area: ['100%', '100%'],
                content: '/admin/sitePage/save',
                success : function(layero, index){
                    //var body = layui.layer.getChildFrame('body', index);
                    setTimeout(function(){
                        layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },500)
                },
                end:function () {
                    admin.getTempData('formOk') && tableIns.reload({},'data');  // 成功刷新表格
                }
            });
            layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize",function(){
                layer.full(index);
            })

        };

        // 修改状态
        form.on('switch(data-table-state)', function (obj) {
            B.post({
                url: '/admin/sitePage/updateState',
                loading: true,
                data: {pageId: obj.elem.value, state: obj.elem.checked ? 0 : 1},
                success: function (data) {
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                    } else {
                        layer.msg(data.msg, {icon: 2});
                        $(obj.elem).prop('checked', !obj.elem.checked);
                        form.render('checkbox');
                    }
                }

            });

        });

    });

</script>
</html>